﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.User
{
    public class Rate
    {
        /// <summary>
        /// Return list of message from user
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.User.Rate> GetListOfRatesFromUser(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR005_ListUserRatesFrom", 0, objListOfParameters);

                List<LimpaGaragem.Model.User.Rate> objListOfRate = new List<LimpaGaragem.Model.User.Rate>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.User.Rate objRate = new LimpaGaragem.Model.User.Rate();
                    objRate.IDUser = IDUser;
                    objRate.ID = Convert.ToInt32(objDataRow["ID_RATE"]);
                    objRate.IDUserRated = Convert.ToInt32(objDataRow["ID_USER_RATED"]);
                    objRate.IDScoreRate = Convert.ToInt32(objDataRow["ID_RATE_SCORE"]);
                    objRate.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfRate.Add(objRate);
                }
                return objListOfRate;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return list of message to user
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.User.Rate> GetListOfRatesToUser(int IDUser)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR005_ListUserRatesTo", 0, objListOfParameters);

                List<LimpaGaragem.Model.User.Rate> objListOfRate = new List<LimpaGaragem.Model.User.Rate>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.User.Rate objRate = new LimpaGaragem.Model.User.Rate();
                    objRate.IDUserRated = IDUser;
                    objRate.ID = Convert.ToInt32(objDataRow["ID_RATE"]);
                    objRate.IDUser = Convert.ToInt32(objDataRow["ID_USER"]);
                    objRate.IDScoreRate = Convert.ToInt32(objDataRow["ID_RATE_SCORE"]);
                    objRate.DateTimeInserted = Convert.ToDateTime(objDataRow["DT_INC"]);
                    objListOfRate.Add(objRate);
                }
                return objListOfRate;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Save rate
        /// </summary>
        public void SaveRate(int IDUserRated,
                            int IDUser,
                            int IDScoreRate)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_USER_RATED", IDUserRated));
                objListOfParameters.Add(new SqlParameter("@ID_USER", IDUser));
                objListOfParameters.Add(new SqlParameter("@ID_RATE_SCORE", IDScoreRate));

                //EXECUTE
                objDataAccess.ExecuteQuery(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR005_SaveUserRate", 0, objListOfParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
